package com.business.api.official.toursism.websiteimage.controller;

import com.business.api.official.toursism.websiteimage.dto.WebsiteImageApiDTO;
import com.business.api.official.toursism.websiteimage.from.WebsiteImageApiSaveFrom;
import com.business.api.official.toursism.websiteimage.from.WebsiteImageApiUpdateFrom;
import com.business.api.official.toursism.websiteimage.manager.WebsiteImageApiManager;
import com.business.api.official.toursism.websiteimage.query.WebsiteImageApiQuery;
import com.business.api.official.toursism.websiteimage.query.WebsiteImageApiPageQuery;
import com.business.api.official.toursism.websiteimage.service.WebsiteImageApiService;
import com.business.api.official.toursism.websiteimage.vo.WebsiteImageApiBriefPageVO;
import com.business.api.official.toursism.websiteimage.vo.WebsiteImageApiDetailPageVO;
import com.business.api.official.toursism.websiteimage.vo.WebsiteImageApiDetailVO;
import com.business.api.official.toursism.websiteimage.vo.WebsiteImageApiDetailListVO;
import com.business.api.official.toursism.websiteimage.vo.data.WebsiteImageApiDetailData;
import com.business.system.official.toursism.websiteimage.entity.WebsiteImage;
import com.seed.core.exception.ManagerException;
import com.seed.core.exception.ServiceException;
import com.seed.core.pojo.controller.SeedApiController;
import com.seed.core.pojo.dto.SeedMessageDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @description 轮播图
 * @author CodeGenerator
 * @date   2025-01-27
 * @version V1.0
 */
@Slf4j
@RestController(value = "websiteImageApiController")
@Api(value = "websiteImageController", tags = "轮播图")
@RequestMapping("/api/v1/toursism/websiteImage")
public class WebsiteImageApiController extends SeedApiController<WebsiteImageApiService, WebsiteImageApiManager, WebsiteImage> {

    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "保存")
    public WebsiteImageApiDetailVO save(@RequestBody @ApiParam(value = "保存表单", required = true) WebsiteImageApiSaveFrom websiteImageFrom) {
        log.info("[api-v1]WebsiteImageController.save,保存");

        WebsiteImageApiDetailVO result = new WebsiteImageApiDetailVO();
        if (StringUtils.isEmpty(websiteImageFrom)) {
            result.setCode("A0410");
            result.setMessage("表单不能为空!");
            result.setSuccess(false);
            return result;
        }
        WebsiteImageApiDTO apiDTO;
        try {
            apiDTO = baseService.save(websiteImageFrom);
        } catch (ServiceException e) {
            log.error("[api-v1]WebsiteImageController.save ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("保存失败");
            return result;
        }
        if (!apiDTO.getSuccess()) {
            result.setCode(apiDTO.getCode());
            result.setSuccess(false);
            result.setMessage(apiDTO.getMessage());
            return result;
        }
        WebsiteImageApiDetailData data;
        try {
            data = baseManager.convertApiDetailData(apiDTO.getWebsiteImage());
        } catch (ManagerException e) {
            log.error("[api-v1]WebsiteImageController.save ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("保存失败");
            return result;
        }
        result.setData(data);
        return result;
    }

    @RequestMapping(method = RequestMethod.PATCH)
    @ResponseBody
    @ApiOperation(value = "不完全更新,更新from中不为空的值（空值不会被更新）")
    public WebsiteImageApiDetailVO updateSelective(@RequestBody @ApiParam(value = "更新表单", required = true) WebsiteImageApiUpdateFrom websiteImageFrom) {
        log.info("[api-v1]WebsiteImageController.updateSelective,不完全更新,更新from中不为空的值（空值不会被更新）");

        WebsiteImageApiDetailVO result = new WebsiteImageApiDetailVO();
        if (StringUtils.isEmpty(websiteImageFrom)) {
            result.setCode("A0410");
            result.setMessage("更新信息不能为空!");
            result.setSuccess(false);
            return result;
        }
        if (StringUtils.isEmpty(websiteImageFrom.getId())) {
            result.setCode("A0410");
            result.setMessage("id信息不能为空!");
            result.setSuccess(false);
            return result;
        }
        WebsiteImageApiDTO apiDTO;
        try {
            apiDTO = baseService.updateSelectiveById(websiteImageFrom);
        } catch (ServiceException e) {
            log.error("[api-v1]WebsiteImageController.updateSelective ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("更新失败");
            return result;
        }
        if (!apiDTO.getSuccess()) {
            result.setCode(apiDTO.getCode());
            result.setSuccess(false);
            result.setMessage(apiDTO.getMessage());
            return result;
        }
        WebsiteImageApiDetailData data;
        try {
            data = baseManager.convertApiDetailData(websiteImageFrom.getId());
        } catch (ManagerException e) {
            log.error("[api-v1]WebsiteImageController.updateSelective ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("更新失败");
            return result;
        }
        result.setData(data);
        return result;
    }

    @RequestMapping(value = "/{ids}", method = RequestMethod.DELETE)
    @ResponseBody
    @ApiOperation(value = "删除,支持ids")
    public WebsiteImageApiDetailVO delete(@PathVariable @ApiParam(value = "主键id", required = true) String ids) {
        log.info("[api-v1]WebsiteImageController.delete,删除");

        WebsiteImageApiDetailVO result = new WebsiteImageApiDetailVO();
        if (StringUtils.isEmpty(ids)) {
            result.setCode("A0410");
            result.setMessage("id不能为空!");
            result.setSuccess(false);
            return result;
        }
        SeedMessageDTO apiDTO;
        try {
            List<String> idList = Arrays.asList(ids.split(","));
            apiDTO = baseService.removeByIds(idList);
        } catch (ServiceException e) {
            log.error("[api-v1]WebsiteImageController.delete ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("删除失败");
            return result;
        }
        if (!apiDTO.getSuccess()) {
            result.setCode(apiDTO.getCode());
            result.setSuccess(false);
            result.setMessage(apiDTO.getMessage());
            return result;
        }
        return result;
    }


    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据id获取轮播图详细信息")
    public WebsiteImageApiDetailVO getDetailVO(@PathVariable @ApiParam(value = "主键id", required = true) String id) {
        log.info("[api-v1]WebsiteImageController.getWebsiteImageApiVO接口,根据id获取轮播图详细信息");

        WebsiteImageApiDetailVO result = new WebsiteImageApiDetailVO();
        if (StringUtils.isEmpty(id)) {
            result.setCode("A0410");
            result.setMessage("id不能为空!");
            result.setSuccess(false);
            return result;
        }

        WebsiteImageApiDetailData data;
        try {
            data = baseManager.convertApiDetailData(id);
        } catch (ManagerException e) {
            log.error("[api-v1]WebsiteImageController.getWebsiteImageApiVO接口 ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("查询失败");
            return result;
        }
        result.setData(data);
        return result;
    }


    @RequestMapping(value = "/all", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取全部")
    public WebsiteImageApiDetailListVO listAll(@ApiParam(value = "查询条件", required = true) WebsiteImageApiQuery query) {
        log.info("[api-v1]WebsiteImageController.listAll,获取全部");
        WebsiteImageApiDetailListVO result = new WebsiteImageApiDetailListVO();
        if (StringUtils.isEmpty(query)) {
            query = new WebsiteImageApiQuery();
        }
        try {
            result = baseService.listAll(query);
        } catch (ServiceException e) {
            log.error("[api-v1]WebsiteImageController.listAll ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("查询失败");
            return result;
        }

        return result;
    }

    @RequestMapping(value = "/brief", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据查询条件查询简略对象")
    public WebsiteImageApiBriefPageVO getBriefPageVO(@ApiParam(value = "查询条件", required = true) WebsiteImageApiPageQuery query) {
        log.info("[api-v1]WebsiteImageController.getBriefPageVO,根据查询条件查询简略对象");

        WebsiteImageApiBriefPageVO result = new WebsiteImageApiBriefPageVO();
        if (StringUtils.isEmpty(query)) {
            query = new WebsiteImageApiPageQuery();
        }

        try {
            result = baseService.getBriefPageVO(query);
        } catch (ServiceException e) {
            log.error("[api-v1]WebsiteImageController.getBriefPageVO ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("查询失败");
            return result;
        }

        return result;
    }


    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据查询条件查询详细对象")
    public WebsiteImageApiDetailPageVO getDetailPageVO(@ApiParam(value = "查询条件", required = true) WebsiteImageApiPageQuery query) {
        log.info("[api-v1]WebsiteImageController.getDetailPageVO,根据查询条件查询详细对象");

        WebsiteImageApiDetailPageVO result = new WebsiteImageApiDetailPageVO();
        if (StringUtils.isEmpty(query)) {
            query = new WebsiteImageApiPageQuery();
        }

        try {
            result = baseService.getDetailPageVO(query);
        } catch (ServiceException e) {
            log.error("[api-v1]WebsiteImageController.getDetailPageVO ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("查询失败");
            return result;
        }

        return result;
    }

}